/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import AccesoADatos.GlobalException;
import AccesoADatos.NoDataException;
import AccesoADatos.ServicioLink;
import Logica.DatosConexion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fernando
 */
public class Servidores extends javax.swing.JPanel {

    private ArrayList<String> Servers;
    private DatosConexion dataConecction;
    private ServicioLink sls;
    private Principal principal;

    public Servidores(DatosConexion cs, Principal p) {
        initComponents();
        this.dataConecction = cs;
        this.principal = p;
        sls = new ServicioLink(dataConecction.getUser(), dataConecction.getPass(), dataConecction.getIp(), dataConecction.getPort(), dataConecction.getDb());

        this.tServers.getTableHeader().setReorderingAllowed(false);
        try {
            Servers = (ArrayList<String>) sls.listarLinks();
            cargarTablaServers();
        } catch (GlobalException ex) {
            JOptionPane.showMessageDialog(this, "m1");
        } catch (NoDataException ex) {
            JOptionPane.showMessageDialog(this, "There is no database links.");
        }


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tServers = new javax.swing.JTable();
        stB = new javax.swing.JButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("NEW");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 370, 80, -1));

        jButton2.setText("EDIT");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 370, 80, -1));

        tServers = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        tServers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"LOCALHOST"}
            },
            new String [] {
                "SERVER NAME"
            }
        ));
        tServers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tServersMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tServers);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(454, 169, 330, 170));

        stB.setText("STRATEGY");
        stB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stBActionPerformed(evt);
            }
        });
        add(stB, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 370, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        CreateServer cs = new CreateServer(this.dataConecction, this);
        cs.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tServersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tServersMouseClicked
        /* int r = tServers.rowAtPoint(evt.getPoint());
         String strategy = (String) tServers.getValueAt(r, 0);
         this.principal.setContentPane(new Strategy(strategy, getDataConecction(), this.principal));
         this.principal.setExtendedState(JFrame.MAXIMIZED_BOTH);
         this.principal.validate();*/
    }//GEN-LAST:event_tServersMouseClicked

    private void stBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stBActionPerformed
        int r = tServers.getSelectedRow();
        if (r == -1) {
            JOptionPane.showMessageDialog(null, "There is not server selected.");
        } else {
            String strategy = (String) tServers.getValueAt(r, 0);
            this.principal.setContentPane(new Strategy(strategy, getDataConecction(), this.principal));
            this.principal.setExtendedState(JFrame.MAXIMIZED_BOTH);
            this.principal.validate();
        }
    }//GEN-LAST:event_stBActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int r = tServers.getSelectedRow();
        if (r == -1) {
            JOptionPane.showMessageDialog(null, "There is not server selected.");
        } else {
            try {
                String p = (String) tServers.getValueAt(r, 0);
                if (p.equals("LOCALHOST")) {
                    JOptionPane.showMessageDialog(null, "You can not edit LOCALHOST in this program.");
                } else {
                    String editable = this.sls.tomarUser(p);
                    EditServer ed = new EditServer(p,editable,this.dataConecction);
                    ed.setVisible(true);
                }

            } catch (GlobalException ex) {
                JOptionPane.showMessageDialog(null, "w1.");
            } catch (NoDataException ex) {
                JOptionPane.showMessageDialog(null, "w2.");
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton stB;
    private javax.swing.JTable tServers;
    // End of variables declaration//GEN-END:variables

    public DatosConexion getDataConecction() {
        return dataConecction;
    }

    public void setDataConecction(DatosConexion dataConecction) {
        this.dataConecction = dataConecction;
    }

    public void actualizaServers() {
        try {
            Servers = (ArrayList<String>) sls.listarLinks();
        } catch (GlobalException ex) {
            JOptionPane.showMessageDialog(this, "m1");
        } catch (NoDataException ex) {
            JOptionPane.showMessageDialog(this, "There is no database links.");
        }
    }

    public void cargarTablaServers() {

        for (int i = 0; i < ((DefaultTableModel) tServers.getModel()).getRowCount();) {
            ((DefaultTableModel) tServers.getModel()).removeRow(i);
        }

        //agrega lo que haya en el arraylist
        ((DefaultTableModel) tServers.getModel()).addRow(new Object[]{"LOCALHOST"});
        for (int i = 0; i < Servers.size(); i++) {
            String ser = Servers.get(i);
            ((DefaultTableModel) tServers.getModel()).addRow(new Object[]{ser});
        }
    }

    public ServicioLink getSls() {
        return sls;
    }

    public void setSls(ServicioLink sls) {
        this.sls = sls;
    }
}
